package com.zbkj.admin.boot.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.zbkj.admin.boot.jackson.MicaJavaTimeModule;
import net.dreamlu.mica.core.utils.DateUtil;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigDecimal;
import java.time.ZoneId;
import java.util.Locale;
import java.util.TimeZone;

/**
 * jackson 配置
 *
 * @author L.cm
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(ObjectMapper.class)
@AutoConfigureBefore(JacksonAutoConfiguration.class)
public class MicaJacksonConfiguration {

	@Bean
	public Jackson2ObjectMapperBuilderCustomizer micaJacksonCustomizer() {
		return builder -> {
			builder.locale(Locale.CHINA);
			builder.modules(new MicaJavaTimeModule());
			builder.timeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
			builder.simpleDateFormat(DateUtil.PATTERN_DATETIME);
			builder.serializerByType(BigDecimal.class, ToStringSerializer.instance);
			builder.serializerByType(Long.class, ToStringSerializer.instance);
			// 注意不处理 featuresToEnable 和 featuresToDisable 避免安全问题
//			builder.findModulesViaServiceLoader(true);
		};
	}

}
